package com.wang.myapplication.base;

/**
 * Created by xhwang on 2018/3/14.
 */

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.wang.myapplication.R;


public abstract class BaseAppCompatActivity extends AppCompatActivity implements
        View.OnClickListener {
//    /**
//     * 是否沉浸状态栏
//     **/
//    private boolean isSetStatusBar = false;
//    /**
//     * 是否允许全屏
//     **/
//    private boolean mAllowFullScreen = false;
//    /**
//     * 是否禁止旋转屏幕
//     **/
//    private boolean isAllowScreenRoate = true;
    /**
     * 当前Activity渲染的视图View
     **/
    private View mContextView = null;
    /**
     * 是否输出日志信息
     **/
    private boolean isDebug;
    private String APP_NAME;
    protected final String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        isDebug = true;
        APP_NAME = "APP_NAME";
        $Log(TAG + "-->onCreate()");
        try {

            Bundle bundle = getIntent().getExtras();
            initParms(bundle);
            mContextView = LayoutInflater.from(this)
                    .inflate(bindLayout(), null);
//            initTopView();
            setContentView(mContextView);
            initView(mContextView);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

            doBusiness(this);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

//    private void initTopView() {
//        if (mAllowFullScreen) {
//            this.getWindow().setFlags(
//                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
//            requestWindowFeature(Window.FEATURE_NO_TITLE);
//        }
//        if (isSetStatusBar) {
//            steepStatusBar();
//        }
//        if (!isAllowScreenRoate) {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//        } else {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        }
//    }

    @Override
    protected void onStart() {
        super.onStart();
        $Log(TAG + "--->onStart()");
    }

    @Override
    protected void onResume() {
        super.onResume();
        $Log(TAG + "--->onResume()");

    }

    @Override
    protected void onPause() {
        super.onPause();
        $Log(TAG + "--->onPause()");
    }

    @Override
    protected void onStop() {
        super.onStop();
        $Log(TAG + "--->onStop()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        $Log(TAG + "--->onDestroy()");
    }


    /**
     * [沉浸状态栏]
     */
    private void steepStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 透明状态栏
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 透明导航栏
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    /**
     * [初始化Bundle参数]
     *
     * @param parms
     */
    public abstract void initParms(Bundle parms);

    /**
     * [绑定布局]
     *
     * @return
     */
    public abstract int bindLayout();


    /**
     * [初始化控件]
     *
     * @param view
     */
    public abstract void initView(final View view);

    /**
     * [业务操作]
     *
     * @param mContext
     */
    public abstract void doBusiness(Context mContext);

    /**
     * View点击
     **/
    public abstract void widgetClick(View v);

    @Override
    public void onClick(View v) {
        if (!isFastClick())
            widgetClick(v);
    }

    /**
     * [页面跳转]
     *
     * @param clz
     */
    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }

    /**
     * [携带数据的页面跳转]
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T $(int resId) {
        return (T) super.findViewById(resId);
    }

    /**
     * [含有Bundle通过Class打开编辑界面]
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

//
//    /**
//     * [是否允许全屏]
//     *
//     * @param allowFullScreen
//     */
//    public void setAllowFullScreen(boolean allowFullScreen) {
//        this.mAllowFullScreen = allowFullScreen;
//    }
//
//    /**
//     * [是否设置沉浸状态栏]
//     */
//    public void setSteepStatusBar(boolean isSetStatusBar) {
//        this.isSetStatusBar = isSetStatusBar;
//    }
//
//    /**
//     * [是否允许屏幕旋转]
//     *
//     * @param isAllowScreenRoate
//     */
//    public void setScreenRoate(boolean isAllowScreenRoate) {
//        this.isAllowScreenRoate = isAllowScreenRoate;
//    }

    /**
     * [日志输出]
     *
     * @param msg
     */
    protected void $Log(String msg) {
        if (isDebug) {
            Log.d(APP_NAME, msg);
        }
    }

    /**
     * [防止快速点击]
     *
     * @return
     */
    long lastClick = 0;

    private boolean isFastClick() {
        boolean isFastClick;

        if (System.currentTimeMillis() - lastClick <= 1000)
            isFastClick = true;
        else
            isFastClick = false;

        lastClick = System.currentTimeMillis();
        return isFastClick;
    }

    public void toast(String text) {
//        ToastUtils.show(text);
    }

    public void toast(int resId) {
//        ToastUtils.show(String.valueOf(resId));
    }


    /**
     * 隐藏软件盘
     */
    public void hideSoftInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        if (getCurrentFocus() != null) {
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    }

    /**
     * 点击软键盘之外的空白处，隐藏软件盘
     *
     * @param ev
     * @return
     */
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
//            View v = getCurrentFocus();
////            if (ToolUtil.isShouldHideInput(v, ev)) {
////
////                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
////                if (imm != null) {
////                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
////                }
////            }
//            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//            if (imm != null) {
//                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
//            }
//            return super.dispatchTouchEvent(ev);
//        }
//        // 必不可少，否则所有的组件都不会有TouchEvent了
//        if (getWindow().superDispatchTouchEvent(ev)) {
//            return true;
//        }
//        return onTouchEvent(ev);
//    }

    /**
     * 显示软键盘
     */
    public void showInputMethod() {
        if (getCurrentFocus() != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            imm.showSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);
        }
    }
}